package com.timothy.survey;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SurveySaxHelp<T> extends DefaultHandler {
	private List<Survey> list;
	private Survey s;
	String nameflag;

	public List<Survey> getList() {
		return this.list;
	}

	@Override
	public void startDocument() throws SAXException {
		this.list = new ArrayList<Survey>();
	}

	@Override
	public void endDocument() throws SAXException {
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		if (qName.equals("survey")) {
			s = new Survey();
			s.setId(new Integer(attributes.getValue("id")));
		}
		this.nameflag = qName;
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if (qName.equals("survey")) {
			if (s != null) {
				list.add(s);
			}
		}
		nameflag = "";
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		if (nameflag.equals("question")) {
			s.setQuestion(new String(ch, start, length));
		} else if (nameflag.equals("answera")) {
			s.setAnswera(new String(new String(ch, start, length)));
		} else if (nameflag.equals("answerb")) {
			s.setAnswerb(new String(new String(ch, start, length)));
		} else if (nameflag.equals("answerc")) {
			s.setAnswerc(new String(new String(ch, start, length)));
		}

	}
}
